<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $color_header, $color_line1, $color_line2;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

$title = $txt['bnt_corpm_title'];

//-------------------------------------------------------------------------------------------------

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Pull planet information into $planetinfo.
$planet_id = (isset($_GET['planet_id'])) ? stripnum($_GET['planet_id']) : '';
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_planets}
	WHERE planet_id = {int:planet_id}',
	array(
		'db_planets' => $dbtables['planets'],
		'planet_id' => $planet_id
	)
);
if ($smcFunc['db_num_rows']($result) > 0)
	$planetinfo = $smcFunc['db_fetch_assoc']($result);
else
	$planetinfo = '';
$smcFunc['db_free_result']($result);

if ($planetinfo['owner'] == $playerinfo['ship_id'] || ($planetinfo['corp'] == $playerinfo['team'] && $playerinfo['team'] > 0))
{
	// Get query string variables
	$plaction = (isset($_GET['plaction'])) ? $_GET['plaction'] : '';
	
	if ($plaction == "planetcorp")
	{
		echo $txt['bnt_corpm_tocorp'], '<br>';
		
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_planets}
			SET corp = {int:team},
				owner = {int:ship_id}
			WHERE planet_id = {int:planet_id}',
			array(
				'db_planets' => $dbtables['planets'],
				'team' => $playerinfo['team'],
				'ship_id' => $playerinfo['ship_id'],
				'planet_id' => $planet_id
			)
		);
		
		$ownership = calc_ownership($playerinfo['sector']);

		if (!empty($ownership))
			echo '<p>', $ownership, '<p>';
	}
	if ($plaction == "planetpersonal")
	{
		echo $txt['bnt_corpm_topersonal'], '<br>';
		
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_planets}
			SET corp = 0,
				owner = {int:ship_id}
			WHERE planet_id = {int:planet_id}',
			array(
				'db_planets' => $dbtables['planets'],
				'ship_id' => $playerinfo['ship_id'],
				'planet_id' => $planet_id
			)
		);
		
		$ownership = calc_ownership($playerinfo['sector']);
		
		// Kick other players off the planet
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET on_planet = "N"
			WHERE on_planet = "Y"
			AND planet_id = {int:planet_id}
			AND ship_id <> {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $playerinfo['ship_id'],
				'planet_id' => $planet_id
			)
		);
		if (!empty($ownership))
			echo '<p>', $ownership, '<p>';
	}
	
	TextMainMenu();
}
else
{
	echo '<br>', $txt['bnt_corpm_exploit'], '<br>';
	TextMainMenu();
}

?>
